package com.common.utils.abs;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.List;

public abstract class AbstractDaoImpl<E,C extends DaoCondition,D extends BaseMapper<E,C>> implements BaseDao<E,C> {
    @Override
    public int updateById(E model) {
        return daoSupport().updateById(model);
    }

    @Override
    public int insert(E model) {
        return daoSupport().insert(model);
    }

    @Override
    public List<E> selectAll() {
        C daoCondition = blankCondition();
        return selectList(daoCondition);
    }

    @Override
    public E selectFirst(C daoCondition) {
        E result = null;
        List<E> list = selectList(daoCondition);
        if(null != list && !list.isEmpty()){
            result = list.get(0);
        }
        return result;
    }

    @Override
    public E selectOneResult(C daoCondition) {
        E result = null;
        List<E> list =selectList(daoCondition);
        if(null != list && !list.isEmpty()){
            if(list.size() == 1){
                result = list.get(0);
            }else {
                throw new RuntimeException("sql查询结果大于1");
            }
        }
        return result;
    }

    @Override
    public List<E> selectList(C daoCondition) {
        D dao = daoSupport();
        return dao.selectByCondition(daoCondition);
    }

    private  void setValue(Object dto, String name, Object value){
        Method[] m = dto.getClass().getMethods();
        try{
            for(int i = 0; i < m.length; i++){
                if(("set"+name).toLowerCase().equals(m[i].getName().toLowerCase())){
                    Parameter[] parameters = m[i].getParameters();
                    Class c = parameters[0].getType();
                    if(c.equals(value.getClass())){
                        m[i].invoke(dto, value);
                    }else {
                        if(value.getClass().equals(String.class)){
                            if(c.equals(Integer.class)){
                                m[i].invoke(dto, Integer.parseInt((String) value));
                            }else if(c.equals(Long.class)){
                                m[i].invoke(dto, Long.parseLong((String) value));
                            }
                        }else if(value.getClass().equals(Integer.class)){
                            if(c.equals(String.class)){
                                m[i].invoke(dto, String.valueOf(value));
                            }else if(c.equals(Long.class)){
                                m[i].invoke(dto, ((Integer)value).longValue());
                            }
                        }else if(value.getClass().equals(Long.class)){
                            if (c.equals(String.class)) {
                                m[i].invoke(dto, String.valueOf(value));
                            } else if (c.equals(Integer.class)) {
                                m[i].invoke(dto, ((Long) value).intValue());
                            }
                        }
                        break;
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected abstract D daoSupport();
    protected abstract C blankCondition();
}
